import React, {useState, useRef, useEffect,} from 'react';
import BaseBottom from './baseBottom';
import BaseLeft from './baseLeft';
import BaseRight from './baseRight';
import World from './world';
import Account from '@component/account';
import {API} from '../../config/api';
import {http} from '../../utils';

import './index.less';

const Home = () => {
	const [amount, setAmount] = useState({});
	const [ele, setEle] = useState({});
	const [ywData, setYwData] = useState({});
	const [hourData, setHourData] = useState({});
	const [fhData, setFhData] = useState({});
	const [yearMonthData, setYearMonthData] = useState({});
	const [monthData, setMonthData] = useState({});
	useEffect(() => {
		getAmount();
		getEle();
		getYwData();
		getHourData();
		getFhData();
		getYearData();
		getMonthData();
	}, []);

	useEffect(() => {

		const timer = setInterval(() => {
			getAmount();
		}, 5000)
		return () => {
			clearInterval(timer)
		};
	}, []);


	//年月发电量 --年
	const getMonthData = async () => {
		const params = {
			data: '1703533,1703527'
		}
		const res = await http.post(API.LOAD_CHART_MonthD, params);
		setMonthData(res);
	}

	//年月发电量 -- 月
	const getYearData = async () => {
		const params = {
			data: 1703525
		}
		const res = await http.post(API.LOAD_CHART_DAY, params);
		setYearMonthData(res);
	}

	//负荷曲线
	const getFhData = async () => {
		const params = {
			data: 1703519,
			ratio: 1
		}
		const res = await http.post(API.LOAD_CHART_HOUR, params);
		setFhData(res);
	}

	//等效利用小时数
	const getHourData = async () => {
		const params = {
			data: 1703539,
			days: 5
		}
		const res = await http.post(API.LOAD_CHART_DAY, params);
		setHourData(res);
	}

	//运维容量走势
	const getYwData = async () => {
		const params = {
			data: 1703521,
			ratio: 0.001,
			num: 6
		}
		const res = await http.post(API.LOAD_CHART_YEAR, params);
		setYwData(res);
	}

	//总发电量，年发电量，实时负荷
	const getAmount = async () => {
		const data = {};
		const res = await http.post(API.GET_REALVAL,
			[{
				"data_class": "Analogue",
				"data_id": 1703523,
				"ele_id": "rt_real_kwh_total",
				"attrs": [
					{"dbattr": "f_value"}
				]
			}, {
				"data_class": "Analogue",
				"data_id": 1703529,
				"ele_id": "rt_real_kwh_year",
				"attrs": [
					{"dbattr": "f_value"}
				]
			}, {
				"data_class": "Analogue",
				"data_id": 1703519,
				"ele_id": "rt_p_total",
				"attrs": [{"dbattr": "f_value"}
				]
			}]);
		res.map(item => {
			if (item.ele_id === 'rt_real_kwh_total') {
				data.total = (item.vals[0].val / 10000).toFixed(2);
			} else if (item.ele_id === 'rt_real_kwh_year') {
				data.year = (item.vals[0].val / 10000).toFixed(2);
			} else if (item.ele_id === 'rt_p_total') {
				data.fh = (item.vals[0].val * 10).toFixed(2);
			}
		})
		setAmount(data);
	}

	//电量统计
	const getEle = async () => {
		const params = [{
			"data_class": "Analogue",
			"data_id": 1703525,
			"ele_id": "rt_real_kwh_day",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1787225,
			"ele_id": "rt_complete_year",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703545,
			"ele_id": "rt_contribution_coal",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703547,
			"ele_id": "rt_contribution_co2",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703549,
			"ele_id": "rt_contribution_tree",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703551,
			"ele_id": "rt_contribution_coal_year",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703553,
			"ele_id": "rt_contribution_co2_year",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703555,
			"ele_id": "rt_contribution_tree_year",
			"attrs": [{"dbattr": "f_value"}]
		}, {
			"data_class": "Analogue",
			"data_id": 1703541,
			"ele_id": "rt_eqtime_month",
			"attrs": [{"dbattr": "f_value"}]
		}, {"data_class": "Analogue", "data_id": 1703543, "ele_id": "rt_eqtime_year", "attrs": [{"dbattr": "f_value"}]}];
		const data = {};
		const res = await http.post(API.GET_REALVAL, params);
		res.map(item => {
			data[item.ele_id] = item.vals[0].val.toFixed(2);
		});
		setEle(data);
	}
	return (
		<div className={'home'}>
			<div className="title">
				<a href="http://120.132.17.103/main/">浙江金贝智慧能源管理平台</a>
			</div>
			{
				Object.keys(ywData).length > 0 && Object.keys(hourData).length > 0 && Object.keys(fhData).length > 0 && Object.keys(yearMonthData).length > 0
				&& Object.keys(monthData).length > 0
				&&
				<BaseBottom ele={ele} ywData={ywData} hourData={hourData} fhData={fhData} yearMonthData={yearMonthData}
										monthData={monthData}/>
			}
			{

				Object.keys(amount).length > 0 && Object.keys(ele).length > 0
				&&
				<BaseLeft amount={amount} ele={ele}/>
			}
			<World/>
			<BaseRight/>
			<Account className={'account'}/>
		</div>
	)
}

export default Home;

